#ifndef DOMAINMODULE_H
#define DOMAINMODULE_H

#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlRelationalTableModel>
#include <QSqlField>
#include <QSqlError>
#include <QSqlRecord>
#include <QTableView>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QMessageBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QButtonGroup>
#include <QPushButton>
#include <QLineEdit>
#include <QDateEdit>
#include <QRadioButton>
#include <QPixmap>
#include <QLabel>
#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>

#include "CustomToolbar.h"

class DomainModule : public QMainWindow
{
    Q_OBJECT

public:
    explicit DomainModule(QWidget *parent = 0);

signals:

public slots:
    void ManageDomainsBtn();
    void UpdateClientList();
    void CorrectViewOfClients();
    void UpdateDomainsList();
    void CorrectViewOfDomains();
    void UpdateListsBtn();
    void LimpiarListaDominios();
    void SeleccionarCliente();
    void SeleccionarDominioClienteActual();

    // Filtros Rápidos
    void FindRazonSocial();
    void FilterRazonSocial();
    void FindDominio();
    void FilterDominio();
    void FindFechaAlta();
    void FilterFechaAlta();
    void FindFechaVto();
    void FilterFechaVto();
    // ABM
    void AltaBtn();
    void BajaBtn();
    void ModificarBtn();
    void GuardarBtn();
    void DomainViewClicked(const QModelIndex &index);
    void CalculateNICExpirationDate();
    void HabilitarGuardarBtn();
    void DeshabilitarABM();

private:
    CustomModulesToolbar *domain_toolbar;
    QAction *admin_dominio;
    QAction *update_lists;
    // Filtros
    QRadioButton *find_razon_social;
    QRadioButton *find_dominio;
    QRadioButton *find_fecha_alta;
    QRadioButton *find_fecha_vto;
    // Botones
    QPushButton *altaBtn;
    QPushButton *bajaBtn;
    QPushButton *modificarBtn;
    QPushButton *guardarBtn;
    // Dominio
    QLineEdit *line_dominio;
    // Principal - Secundario
    QRadioButton *principal;
    QRadioButton *secundario;
    // Fechas
    QDate *date;
    QDateEdit *fecha_alta;
    QDateEdit *fecha_vto;
    // cPanel y Reg NIC
    QLineEdit *line_cPanelUs;
    QLineEdit *line_cPanelPass;
    QLineEdit *line_regNIC;
    QLineEdit *line_emailRegNIC;

    QLineEdit *find_line;

    QString cliente;
    QString idDominio;
    QString filtro_cliente;
    QString filtro_dominio;

    bool on_add_domain;
    bool on_edit_domain;
    int row_edited;

    void ManageDomainsToolBar();
    void UpdateListsToolBar();
};

class DominioSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    DominioSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        QDate date;
        QDate expDate;
        int dia1, dia2;
        int mes1, mes2;
        int anio1, anio2;
        dia1 = date.currentDate().day();
        mes1 = date.currentDate().month();
        anio1 = date.currentDate().year();
        date.setDate(anio1,mes1,dia1);
        dia2 = date.currentDate().addDays(16).day();
        if (dia2 < dia1) {
            mes2 = date.addMonths(1).month();
            if (mes2 < mes1) {
                anio2 =  date.addYears(1).year();
                expDate.setDate(anio2, mes2, dia2);
            }
            else
                expDate.setDate(anio1, mes2, dia2);
        }
        else
            expDate.setDate(anio1, mes1, dia2);

        QString format = "yyyy-MM-dd";
        QString fechaLimite = expDate.toString(format);

        if(role == Qt::BackgroundColorRole)
        {
            if (index.column() != 5) {
                QModelIndex index2 = index.model()->index(index.row(), 5, index.parent()); // get index of "watched" column
                if (index2.data(Qt::DisplayRole).toString() <= fechaLimite) {
                    return QVariant(QColor(Qt::red));
                }
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() <= fechaLimite) {
                    return QVariant(QColor(Qt::red));
                }
            }
        }
        // Alineación texto
        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 3 || col == 4 || col == 5) {
                return QVariant (Qt::AlignVCenter | Qt::AlignHCenter);
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

class CDominioSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    CDominioSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        QSqlRelationalTableModel *dominios_model = new QSqlRelationalTableModel();

        QDate date;
        QDate expDate;
        int dia1, dia2;
        int mes1, mes2;
        int anio1, anio2;
        dia1 = date.currentDate().day();
        mes1 = date.currentDate().month();
        anio1 = date.currentDate().year();
        date.setDate(anio1,mes1,dia1);
        dia2 = date.currentDate().addDays(16).day();
        if (dia2 < dia1) {
            mes2 = date.addMonths(1).month();
            if (mes2 < mes1) {
                anio2 =  date.addYears(1).year();
                expDate.setDate(anio2, mes2, dia2);
            }
            else
                expDate.setDate(anio1, mes2, dia2);
        }
        else
            expDate.setDate(anio1, mes1, dia2);

        QString format = "yyyy-MM-dd";
        QString fechaLimite = expDate.toString(format);
        QString filtroFecha = "fecha_vencimiento_nic <= '" + fechaLimite + "'";

        if(role == Qt::BackgroundColorRole)
        {
            if (index.column() == 1) {
                QModelIndex index2 = index.model()->index(index.row(), 1, index.parent()); // get index of "watched" column

                QString razonSocial = index2.data(Qt::DisplayRole).toString();
                QString Filtro = "d_razon_social = '" + razonSocial + "' and " + filtroFecha;
                dominios_model->setTable("dominio");
                dominios_model->setFilter(Filtro);
                QSqlDatabase().transaction();
                if (!dominios_model->select()) {
                    QSqlDatabase().rollback();
                }
                else {
                    QSqlDatabase().commit();
                    QString rSocial;
                    int rowCount = dominios_model->rowCount();
                    for (int i = 0; i < rowCount; ++i) {
                        rSocial = dominios_model->index(i, 1, index.parent()).data(Qt::DisplayRole).toString();
                        if (index2.data(Qt::DisplayRole).toString() == rSocial) {
                            return QVariant(QColor(Qt::red));
                        }
                    }
                }
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // DOMAINMODULE_H
